/*
 * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef SLE_SSAP_SERVER_HDI
#define SLE_SSAP_SERVER_HDI

#include <stdbool.h>
#include <stdint.h>
#include "sle_hal_errcode.h"
#include "sle_ssap_config.h"
#include "sle_ssap_server_config.h"

#ifdef _Cplusplus
extern "C" {
#endif

/**
 * @brief Callback Invoked When Service Started.
 * 
 * @param [in] serverId Server ID.
 * @param [in] handle    Service Attribute Handle.
 * @param [in] errCode   Error Code.
 * @return #void No Return Value.
 */
typedef void (*SleHdiSsapsStartServiceCallback)(uint8_t serverId, uint16_t handle, SleHdiErrorCode errCode);

/**
 * @brief Callback Invoked When All Services Deleted.
 * 
 * @param [in] serverId Server ID.
 * @param [in] errCode         Error Code.
 * @return #void No Return Value.
 */
typedef void (*SleHdiSsapsDeleteAllServiceCallback)(uint8_t serverId, SleHdiErrorCode errCode);

/**
 * @brief Callback Invoked When Receive Read Request.
 * 
 * @param [in] serverId     Server ID.
 * @param [in] connectId       Connection ID.
 * @param [in] ReadCbPara   Read Request Parameter.
 * @param [in] errCode      Error Code.
 * @return #void No Return Value.
 */
typedef void (*SleHdiSsapsReadRequestCallback)(uint8_t serverId, uint16_t connectId, const SsapsReqReadCbParam *readCbPara,
    SleHdiErrorCode errCode);

/**
 * @brief Callback Invoked When Receive Write Request.
 * 
 * @param [in] serverId     Server ID.
 * @param [in] connectId       Connection ID.
 * @param [in] writeCbPara  Write Request Parameter.
 * @param [in] errCode      Error Code.
 * @return #void No Return Value.
 */
typedef void (*SleHdiSsapsWriteRequestCallback)(uint8_t serverId, uint16_t connectId, const SsapsReqWriteCbPara *writeCbPara,
    SleHdiErrorCode errCode);

/**
 * @brief Callback Invoked When Mtu Size Changed.
 * 
 * @param [in] serverId Server ID.
 * @param [in] connectId   Connection ID.
 * @param [in] mtuSize  Mtu Size.
 * @param [in] errCode         Error Code.
 * @return #void No Return Value.
 */
typedef void (*SleHdiSsapsMtuChangedCallback)(uint8_t serverId, uint16_t connectId,
    const SsapMtuInfo *info, SleHdiErrorCode errCode);

/**
 * @brief struct Of SSAP Server Callback Function.
 */
typedef struct {
    SleHdiSsapsStartServiceCallback         OnServiceStartedCb;             /*!< @if Eng Service Started Callback.
                                                                            @else   启动服务回调函数。 @endif */
    SleHdiSsapsDeleteAllServiceCallback     OnAllServiceRemovedCb;          /*!< @if Eng Service Deleted Callback.
                                                                            @else   删除服务回调函数。 @endif */
    SleHdiSsapsReadRequestCallback          OnRecvReadReqCb;                /*!< @if Eng Read Request Received Callback.
                                                                            @else   收到远端读请求回调函数。 @endif */
    SleHdiSsapsWriteRequestCallback         OnRecvWriteReqCb;                   /*!< @if Eng Write Request Received Callback.
                                                                            @else   收到远端写请求回调函数。 @endif */
    SleHdiSsapsMtuChangedCallback           OnMtuChangedCb;                 /*!< @if Eng Mtu Changed Callback.
                                                                            @else   Mtu 大小更新回调函数。 @endif */
} SleSsapsHdiCallbacks;

/**
 * @brief  Register Ssap Server.
 * 
 * @param  [in]  AppUuid  App uuid
 * @param  [out] serverId Server ID
 * @return Error Code.
 */
typedef SleHdiErrorCode (*SleHdiSsapsRegisterServer)(const SleUuid *appUuid, uint8_t *serverId);

/**
 * @brief  Unregister Ssap Server.
 *
 * @param  [in] serverId Server ID
 * @return Error Code.
 */
typedef SleHdiErrorCode (*SleHdiSsapsUnregisterServer)(uint8_t serverId);

/**
 * @brief  Add A Ssap Service sync.
 * 
 * @param  [in]  serverId Server ID.
 * @param  [in]  ServiceUuid Service uuid.
 * @param  [in]  IsPrimary Is Primary Service Or Not.
 * @param  [out] Handle Service Handle.
 * @return Error Code.
 */
typedef SleHdiErrorCode (*SleHdiSsapsAddService)(uint8_t serverId, const SleUuid *ServiceUuid, bool IsPrimary, uint16_t *handle);

/**
 * @brief  Add A Ssap Property sync.
 * 
 * @param  [in]  serverId      Server ID.
 * @param  [in]  serviceHandle Service Handle.
 * @param  [in]  Property      SSAP Properity Info.
 * @param  [out] Handle         Properity Handle.
 * @return Error Code.
 */
typedef SleHdiErrorCode (*SleHdiSsapsAddProperty)(uint8_t serverId, uint16_t serviceHandle, const SsapsPropertyParam *property,
    uint16_t *handle);

/**
 * @brief  Add A Ssap Property Descriptor sync.
 * 
 * @param  [in]  serverId       Server ID.
 * @param  [in]  serviceHandle  Service Handle.
 * @param  [in]  propertyHandle Properity Handle Corresponding To The Descriptor.
 * @param  [in]  Descriptor      Properity Descriptor.
 * @param  [out] Handle          Descriptor Handle.
 * @return Error Code.
 */
typedef SleHdiErrorCode (*SleHdiSsapsAddDescriptor)(uint8_t serverId, uint16_t serviceHandle, uint16_t propertyHandle,
    const SsapsDescriptorParam *descriptor, uint16_t *handle);

/**
 * @brief  Start A SSAP Service.
 * 
 * @param  [in] serverId      Server ID.
 * @param  [in] serviceHandle Service Handle.
 * @return Error Code, The Service Start Result Will Be Returned At { @ref SsapsStartServiceCallback }.
 */
typedef SleHdiErrorCode (*SleHdiSsapsStartService)(uint8_t serverId, uint16_t serviceHandle);

/**
 * @brief  Delete All SSAP Service.
 * 
 * @param  [in] serverId      Server ID.
 * @return Error Code.
 */
typedef SleHdiErrorCode (*SleHdiSsapsDeleteAllServices)(uint8_t serverId);

/**
 * @brief  Send Response When Receive The Request Need To Response By User.
 * 
 * @param  [in] serverId Server ID.
 * @param  [in] connectId   Connection ID.
 * @param  [in] Param     Response Parameter.
 * @return Error Code.
 */
typedef SleHdiErrorCode (*SleHdiSsapsSendResponse)(uint8_t serverId, uint16_t connectId, const SsapsSendRspParam *param);

/**
 * @brief  Send Indication Or Notification To Remote Device.
 * 
 * @param  [in] serverId Server ID.
 * @param  [in] connectId   Connection ID，To Send Packets To All Peer Ends, Enter connectId = 0xffff.
 * @param  [in] Param     Notify/indicate Parameter.
 * @return Error Code.
 */
typedef SleHdiErrorCode (*SleHdiSsapsNotifyIndicate)(uint8_t serverId, uint16_t connectId, const SsapsNotifyParam *param);

/**
 * @brief  Send Indication Or Notification To Remote Device By uuid.
 * 
 * @param  [in] serverId Server ID.
 * @param  [in] connectId   Connection ID，To Send Packets To All Peer Ends, Enter connectId = 0xffff.
 * @param  [in] Param     Notify/indicate Parameter.
 * @return Error Code.
 */
typedef SleHdiErrorCode (*SleHdiSsapsNotifyIndicateByUuid)(uint8_t serverId, uint16_t connectId, const SsapsNotyfyByUuidParam *param);


/**
 * @brief  Set Server Info Before Connected.
 * 
 * @param  [in] serverId Server ID.
 * @param  [in] Info      Server Info.
 * @return Error Code.
 */
typedef SleHdiErrorCode (*SleHdiSsapsSetInfo)(uint8_t serverId, const SsapMtuInfo *info);

/**
 * @brief  Register SSAP Server Callbacks.
 * 
 * @param  [in] Func Callback Function.
 * @return Error Code.
 */
typedef SleHdiErrorCode (*SleHdiSsapsRegisterCallbacks)(SleSsapsHdiCallbacks *callbacks);

/**
 * @brief struct Of SSAP Server Function.
 */
typedef struct {
    SleHdiSsapsRegisterServer           registerServer;
    SleHdiSsapsUnregisterServer         unregisterServer;
    SleHdiSsapsAddService               addService;
    SleHdiSsapsAddProperty              addProperty;
    SleHdiSsapsAddDescriptor            addDescriptor;
    SleHdiSsapsStartService             startService;
    SleHdiSsapsDeleteAllServices        deleteAllServices;
    SleHdiSsapsSendResponse             sendResponse;
    SleHdiSsapsNotifyIndicate           sendNotifyIndicate;
    SleHdiSsapsNotifyIndicateByUuid     sendNotifyIndicateByUuid;
    SleHdiSsapsSetInfo                  setServerMtuInfo;
    SleHdiSsapsRegisterCallbacks        registerServerCbks;
} SleSsapsHdiOpts;

/**
 * @brief  Get Ssaps client options.
 * 
 * @param [out] ssapsOpts Ssaps client options.
 * @return error code.
 */
SleHdiErrorCode GetSleSsapsHdiOpts(SleSsapsHdiOpts *ssapsOpts);


#ifdef _Cplusplus
}
#endif
#endif /* SLE_SSAP_SERVER_HDI */

